package com.sunyur.tailor.channel.service.inner.purchase.service.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.base.SunyurEventService;
import com.sunyur.tailor.channel.service.middle.purchase.MiddlePurchaseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Author pengbing
 * @Description 申请单消息入口
 * @Date 20:21 2021/7/8
 * @Param
 * @return
 **/
public class SunyurPurchaseApplyEntranceServiceImpl implements SunyurEventService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurPurchaseApplyEntranceServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurPurchaseApplyEntranceServiceImpl.class);

    @Autowired
    MiddlePurchaseService middlePurchaseService;


    public SunyurResult create(String param) throws Exception {
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl create param:{}", param);
        SunyurResult result = new SunyurResult<>();
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        } else {
            MsgResultCode msgResultCode = JSON.parseObject(param, MsgResultCode.class);
            ChannelResult channelResult = middlePurchaseService.syncCreate(msgResultCode);
            if (ChannelResultUtil.isSuccess(channelResult)) {
                result.setContent(Boolean.TRUE);
                result.setCode(MiddleResultEnum.SUCCESS.getCode());
            } else {
                result.setErrorMessage(channelResult.getCode(), channelResult.getMessage());
            }
        }
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl create return:{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * @Author XuHui
     * @Description 请购流程事件
     * @Date 16:46 2021/12/28
     **/
    public SunyurResult process(String param) throws Exception {
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl process param:{}", param);
        SunyurResult result = new SunyurResult<>();
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        } else {
            MsgResultCode msgResultCode = JSON.parseObject(param, MsgResultCode.class);
            ChannelResult channelResult = middlePurchaseService.process(msgResultCode);
            if (ChannelResultUtil.isSuccess(channelResult)) {
                result.setContent(Boolean.TRUE);
                result.setCode(MiddleResultEnum.SUCCESS.getCode());
            } else {
                result.setErrorMessage(channelResult.getCode(), channelResult.getMessage());
            }
        }
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl process return:{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * @Author XuHui
     * @Description 需求行处理事件
     * @Date 16:46 2021/12/28
     **/
    public SunyurResult demandOperate(String param) throws Exception {
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl demandOperate param:{}", param);
        SunyurResult result = new SunyurResult<>();
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        } else {
            MsgResultCode msgResultCode = JSON.parseObject(param, MsgResultCode.class);
            ChannelResult channelResult = middlePurchaseService.demandOperate(msgResultCode);
            if (ChannelResultUtil.isSuccess(channelResult)) {
                result.setContent(Boolean.TRUE);
                result.setCode(MiddleResultEnum.SUCCESS.getCode());
            } else {
                result.setErrorMessage(channelResult.getCode(), channelResult.getMessage());
            }
        }
        LOGGER_INFO.info("SunyurPurchaseApplyEntranceServiceImpl demandOperate return:{}", JSON.toJSONString(result));
        return result;
    }


}
